/**
 * @author mmbro
 * @homepage https://gitee.com/mmbro/
 * @date 2021年05月18日01:19:32
 * @description 注释:字符串工具类
 */
StringUtil = class StringUtil {

    /**
     * @description 填充字符串到新的长度
     * @param str 源字符串
     * @param length 新长度
     * @param fill 填充内容(可选,不传默认空格填充)
     * @returns {string}
     */
    static newLength(str, length, fill) {
        str = String(str);
        fill = !fill || !fill ? ' ' : String(fill);
        fill = fill.length === 0 || !fill ? ' ' : fill;
        length = isNaN(length) || !length ? str.length : length;
        if (str.length > length) {
            str = str.substr(0, length);
        } else {
            let blankArr = [fill];
            let newLen = fill.length;
            let fillLen = length - str.length;
            while (newLen < fillLen) {
                blankArr.push(fill);
                newLen += fill.length;
            }
            let blankStr = blankArr.join('');
            if (blankStr.length > fillLen) {
                blankStr = blankStr.substr(0, fillLen);
            }
            str = str + blankStr;
        }
        return str;
    }
}